<?php

namespace App\Repository\Repositories;

use App\Repository\Repositories\Interfaces\PermissionRepository;
use App\Repository\Models\Role;
use App\Repository\Repositories\Interfaces\RoleRepository;

/**
 * Class RoleRepositoryEloquent.
 *
 * @package namespace App\Repository\Repositories;
 */
class RoleRepositoryEloquent extends BaseRepository implements RoleRepository
{
    /**
     * 搜索
     * @var array
     */
    protected $fieldSearchable = [
    ];

    /**
     * Specify Model class name
     *
     * @return string
     */
    public function model()
    {
        return Role::class;
    }

    /**
     * 根据角色模型获取分组权限
     *
     * @param $role
     *
     * @return array
     */
    public function getTypeGroupPermissionsByRoleModel($role)
    {
        $data = [];
        $permissions = app(PermissionRepository::class)->all()['data'];
        $rolePermission = $role->perms()->pluck('id')->toArray();

        foreach ($permissions as $key => $permission) {
            $data[$key]['permission_id'] = $permission['permission_id'];
            $data[$key]['type'] = $permission['type'];
            $data[$key]['name'] = $permission['display_name'] . '(' . $permission['name'] . ')';
            $data[$key]['checked'] = 0;
            in_array($permission['permission_id'], $rolePermission) && $data[$key]['checked'] = 1;
        }
        return $data;
    }
    
}
